import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import Export from '../common/Export';
import {PERMISSION} from './constants';
import {transportType} from '../Enum';
import {FORM_OPTIONS} from '../constants';
import {injectIntl, language} from './intl';
import TextArea from 'Shared/components/TextArea';
import {hasPermissions, ListTypeCL} from './utils';
import TextInput from 'Shared/components/TextInput';
import WrappedSelect from '../common/WrappedSelect';
import WrappedInputPercent from '../common/WrappedInputPercent';
import DateRangePicker from 'Shared/components/DateRangePicker';
import {Button, Form, Row, Col, Card} from 'antd';
const FormItem = Form.Item;
const widths = {
    width: '40%',
    textAlign: 'center'
};
class QueryPanel extends PureComponent {
    handleFilterChange = (value, name) => {
        this.props.onConditionsChange(name, value);
    }
    handleFilterPressEnter = (value, name) => {
        this.props.onConditionsChange(name, value);
        this.props.querySearch();
    }
    render() {
        const {formatMessage} = this.props.intl;
        const {conditions, permissions, querySearch,
            onResetClick, onClickExport, loading} = this.props;
        const operations = [];
        const {marketDepartments, partSaleOrderTypes} = this.props.initEnumData;
        if(hasPermissions(permissions, PERMISSION.export))
            operations.push(
                <Export key={PERMISSION.export} exportRequest={onClickExport}/>
            );
        return (
            <div>
                <Form className="form-standard">
                    <Card>
                        <Row>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.marketDepartment',
                                    defaultMessage: '大区'
                                })}
                                {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="marketDepartmentId"
                                        options={marketDepartments}
                                        value={conditions.marketDepartmentId}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.dealerCode',
                                    defaultMessage: 'ERP号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="dealerCode"
                                        value={conditions.dealerCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.partSaleOrderType',
                                    defaultMessage: '订单类型'
                                })}
                                {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="partOrderTypeId"
                                        options={partSaleOrderTypes}
                                        value={conditions.partOrderTypeId}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.codes',
                                        defaultMessage: '订单号'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <TextArea
                                        name="codes"
                                        placeholder={formatMessage({
                                            id: 'queryPanel.form.codes.placeholder',
                                            defaultMessage: '最多输入1000个，以回车隔开'
                                        })}
                                        value={conditions.codes}
                                        onBlur={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.submitDate',
                                        defaultMessage: '订单提报日期'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <DateRangePicker
                                        name="submitDate"
                                        value={conditions.submitDate}
                                        onChange={this.handleFilterChange} />
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.partInvoiceCode',
                                    defaultMessage: '发票号'
                                })}{...FORM_OPTIONS.item}>
                                    <TextInput
                                        name="partInvoiceCode"
                                        value={conditions.partInvoiceCode}
                                        onPressEnter={this.handleFilterPressEnter}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem label={formatMessage({
                                    id: 'queryPanel.form.transportType',
                                    defaultMessage: '运输方式'
                                })}
                                {...FORM_OPTIONS.item}>
                                    <WrappedSelect
                                        allowClear
                                        name="transportType"
                                        options={transportType.toList(language)}
                                        value={conditions.transportType}
                                        onChange={this.handleFilterChange}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.addupVarietyRate',
                                        defaultMessage: '累计品种满足率'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedInputPercent
                                        style={widths}
                                        percent={true}
                                        onChange={this.handleFilterChange}
                                        name={['beginAddupVarietyRate', 'endAddupVarietyRate']}
                                        value={[conditions.beginAddupVarietyRate, conditions.endAddupVarietyRate]}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.addupMoneyRate',
                                        defaultMessage: '累计金额满足率'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedInputPercent
                                        style={widths}
                                        percent={true}
                                        onChange={this.handleFilterChange}
                                        name={['beginAddupMoneyRate', 'endAddupMoneyRate']}
                                        value={[conditions.beginAddupMoneyRate, conditions.endAddupMoneyRate]}/>
                                </FormItem>
                            </Col>
                            <Col {...FORM_OPTIONS.col}>
                                <FormItem
                                    label={formatMessage({
                                        id: 'queryPanel.form.addupQuantityRate',
                                        defaultMessage: '累计数量满足率'
                                    })}
                                    {...FORM_OPTIONS.item}>
                                    <WrappedInputPercent
                                        style={widths}
                                        percent={true}
                                        onChange={this.handleFilterChange}
                                        name={['beginAddupQuantityRate', 'endAddupQuantityRate']}
                                        value={[conditions.beginAddupQuantityRate, conditions.endAddupQuantityRate]}/>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row>
                            <Col span={6}>
                                <Button key="query" type="primary" onClick={querySearch} loading={loading}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.query',
                                        defaultMessage: '查询'
                                    })}</Button>
                                <Button key="clear" onClick={onResetClick}>
                                    {formatMessage({
                                        id: 'queryPanel.btn.reset',
                                        defaultMessage: '重置'
                                    })}</Button>
                            </Col>
                            <Col span={18} className="col-align-right">
                                {operations}
                            </Col>
                        </Row>
                    </Card>
                </Form>
            </div>
        );
    }
}

QueryPanel.propTypes = {
    conditions: PropTypes.object.isRequired,
    initEnumData: PropTypes.object.isRequired,
    loading: PropTypes.bool.isRequired,
    permissions: PropTypes.array.isRequired,
    querySearch: PropTypes.func.isRequired,
    onClickExport: PropTypes.func.isRequired,
    onConditionsChange: PropTypes.func.isRequired,
    onResetClick: PropTypes.func.isRequired,
    intl: PropTypes.object,
};
import {connect} from 'react-redux';
import {querySearch, resetQueryPanel, saveQueryCondition, exportData} from './actions';
import {createSelector} from 'reselect';
import selectorFactory from 'Shared/utils/immutableToJsSelectorFactory';
const getinitEnumData = createSelector(
    state => state.getIn(['page', 'domainData', 'init']),
    init => {
        const initData = init.toJS();
        const marketDepartments = initData.marketDepartments ? ListTypeCL(initData.marketDepartments) : [];
        const partSaleOrderTypes = initData.partSaleOrderTypes ? ListTypeCL(initData.partSaleOrderTypes) : [];
        return {
            initData,
            marketDepartments,
            partSaleOrderTypes
        };
    }
);

const getPermissions = selectorFactory(['page', 'domainData', 'permission', 'data']);
const getConditions = selectorFactory(['page', 'appState', 'queryCondition']);
const mapStateToProps = state => ({
    conditions: getConditions(state),
    initEnumData: getinitEnumData(state),
    loading: state.getIn(['page', 'domainData', 'list', 'isFetching']),
    permissions: getPermissions(state),
});

const mapDispatchToProps = dispatch => ({
    onConditionsChange: (name, value) => dispatch(saveQueryCondition(name, value)),
    querySearch: () => dispatch(querySearch()),
    onResetClick: () => dispatch(resetQueryPanel()),
    onClickExport: () => dispatch(exportData())
});

export default connect(mapStateToProps, mapDispatchToProps)(injectIntl(QueryPanel));
